﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Windows.Media.Imaging;
using System.IO;

namespace TinyCinema.Data
{
    public class Employee : Execution
    {
        public int _Index;
        public int Index 
        {
            get
            {
                return _Index;
            }
            set
            {
                _Index = value;
                OnPropertyChanged("Index");
            }
        }
        public string ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string Hometown { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public long Phone { get; set; }
        public long CardNo { get; set; }
        public DateTime StartDate { get; set; }
        bool IsIniting = true;
        public Employee Manager { get; set; }
        public string _Password;
        public string Password
        {
            get { return _Password; }
            set
            {
                if (!IsIniting)
                {
                    _Password = Encryption.CreateMD5Hash(value);
                    Save();
                }
                else _Password = value;
            }
        }
        public Position Position { get; set; }
        public Department Department { get; set; }
        public Image Photo { get; set; }
        public ObservableCollection<MenuOrder> MenuOrders { get; set; }
        public ReadOnlyObservableCollection<TicketOrder> TicketOrders { get; set; }
        public static ObservableCollection<Employee> GetAllEmployee()
        {           
            PutTableIntoDataSet(Tables.Employee);
            var tbl = ds.Tables[Tables.Employee];
            var Rows = tbl.Select();
            var Result = new ObservableCollection<Employee>();
            foreach (var Row in Rows)
            {
                var x = Row[EmployeeTable.ID] as String;
                var emp = new Employee(x);
                Result.Add(emp);
            }
            return Result;
        }
        public Employee() 
        {
            ID = IDGenerate(Tables.Employee);
            Position = new Position(0);
            Department = new Department(0);
            Photo = new Image();
            Manager = null;
            IsIniting = false;
        }
        public Employee(string _ID)
        {
            if (_ID == null) return;
            ID = _ID;
            Index = -1;
            Photo = new Image();
            PutTableIntoDataSet(Tables.Employee);
            var tbl = ds.Tables[Tables.Employee];
            var Row = tbl.Select(EmployeeTable.ID + "='" + ID + "'")[0];
            Name = Row[EmployeeTable.Name] as string;
            Gender = Row[EmployeeTable.Gender] as string;
            DateOfBirth = (DateTime)Row[EmployeeTable.DateOfBirth];
            Hometown = Row[EmployeeTable.Hometown] as string;

            Address = Row[EmployeeTable.Address] as string;
            Email = Row[EmployeeTable.Email] as string;
            Phone = long.Parse(Row[EmployeeTable.Phone] as string);
            foreach (var row in tbl.Select())
            {
                if ((row[EmployeeTable.ID] as string) != ID)
                    ++Index;
                else
                {
                    ++Index;
                    break;
                }
            }
            try
            {
                CardNo = long.Parse(Row[EmployeeTable.CardNo] as string);
            }
            catch 
            {
                CardNo = 0;
            }
            StartDate = (DateTime)Row[EmployeeTable.StartDate];
            Password = Row[EmployeeTable.Password] as string;
            Department = new Department(Row[DepartmentTable.ID] as string);
            try
            {
                Photo = new Image(Row[EmployeeTable.Photo] as String);
            }
            catch { }
            Position = new Position(Row[EmployeeTable.PositionID] as string);
            if (Position.IsManager)
            {
                Manager = this;
                //Save();
            }
            else
            {
                try
                {
                    Manager = new Employee(Row[EmployeeTable.ManagerID] as string);
                    //if (!Manager.Position.IsManager)
                    //{
                    //    Manager = new Employee();
                    //    Manager.ID = null;
                    //    //Save();
                    //}
                }
                catch { }
            }

            IsIniting = false;
        }
        public void AddPhoto()
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Image|*.jpg";
            if (dialog.ShowDialog().Value)
            {
                MemoryStream memoryStream = new MemoryStream();
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(new BitmapImage(new Uri(dialog.FileName))));
                encoder.Save(memoryStream);
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
                Photo.DisplayImage = bitmapImage;
            }
        }
        public void Save()
        {
            using (var tbl = ds.Tables[Tables.Employee])
            {
                var Rows = ds.Tables[Tables.Employee].Select(EmployeeTable.ID + "='" + ID + "'");
                if (Rows.Length==0)
                {

                    var Row = tbl.NewRow();
                    Row[EmployeeTable.ID] = ID;
                    Row[EmployeeTable.Name] = Name;
                    Row[EmployeeTable.Gender] = Gender;
                    Row[EmployeeTable.DateOfBirth] = DateOfBirth;
                    Row[EmployeeTable.Hometown] = Hometown;
                    Row[EmployeeTable.Address] = Address;
                    Row[EmployeeTable.Email] = Email;
                    Row[EmployeeTable.Phone] = Phone;
                    Row[EmployeeTable.CardNo] = CardNo;
                    Row[EmployeeTable.StartDate] = StartDate;
                    Row[EmployeeTable.Password] = Password;
                    Row[EmployeeTable.PositionID] = Position.ID;
                    Row[EmployeeTable.DepartmentID] = Department.ID;
                    try
                    {
                        Row[EmployeeTable.ManagerID] = tbl.Select()[Manager.Index][EmployeeTable.ID];
                        Manager = new Employee(Row[EmployeeTable.ManagerID] as string);
                    }
                    catch { }
                    Row[EmployeeTable.Photo] = Photo.ToBase64String();

                    tbl.Rows.Add(Row);

                    SaveCommand(Tables.Employee);
                }
                else
                {
                    foreach (System.Data.DataRow Row in tbl.Rows)
                        if ((Row[EmployeeTable.ID] as String) == ID)
                        {
                            Row[EmployeeTable.Name] = Name;
                            Row[EmployeeTable.Gender] = Gender;
                            Row[EmployeeTable.DateOfBirth] = DateOfBirth;
                            Row[EmployeeTable.Hometown] = Hometown;
                            Row[EmployeeTable.Address] = Address;
                            Row[EmployeeTable.Email] = Email;
                            Row[EmployeeTable.Phone] = Phone;
                            Row[EmployeeTable.CardNo] = CardNo;
                            Row[EmployeeTable.StartDate] = StartDate;
                            Row[EmployeeTable.Password] = Password;
                            Row[EmployeeTable.PositionID] = Position.ID;
                            Row[EmployeeTable.DepartmentID] = Department.ID;
                            Row[EmployeeTable.ManagerID] = tbl.Select()[Manager.Index][EmployeeTable.ID];                            
                            Row[EmployeeTable.Photo] = Photo.ToBase64String();

                            SaveCommand(Tables.Employee);
                            break;
                        }
                }
            }
        }
        public void Delete()
        {
            using (var tbl = ds.Tables[Tables.Employee])
            {
                string Condition = EmployeeTable.ID + "='" + ID + "'";
                tbl.Select(Condition)[0].Delete();

                SaveCommand(Tables.Employee);
            }
        }
        private static ObservableCollection<Employee> RowsToEmployees(System.Data.DataRow[] Rows)
        {
            var Result = new ObservableCollection<Employee>();
            foreach (var Row in Rows)
                Result.Add(new Employee(Row[EmployeeTable.ID] as String));
            return Result;
        }
        public static ObservableCollection<Employee> SearchByID(string str)
        {
            PutTableIntoDataSet(Tables.Employee);
            var tbl = ds.Tables[Tables.Employee];
            var Rows = tbl.Select(EmployeeTable.ID + " Like '%" + str + "%'");
            return RowsToEmployees(Rows);
        }
        public static ObservableCollection<Employee> SearchByName(string str)
        {
            PutTableIntoDataSet(Tables.Employee);
            var tbl = ds.Tables[Tables.Employee];
            var Rows = tbl.Select(EmployeeTable.Name + " Like '%" + str + "%'");
            return RowsToEmployees(Rows);
        }
    }
}
